/**
 * 
 */
package com.googlecode.hifl;

import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

/**
 * @author root
 *
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
public class BlockInputFormat extends FileInputFormat<LongWritable, Text> {

	public static final String LINES_PER_MAP = "mapreduce.input.blockinputformat.linespermap";
	public static final String ROWS_PER_MAP = "mapreduce.input.blockinputformat.rowspermap";
	
	@Override
	public RecordReader<LongWritable, Text> createRecordReader(InputSplit arg0,
			TaskAttemptContext arg1) throws IOException, InterruptedException {
		return new BlockRecordReader();
	}

	
	/**
	 * Set the number of lines per split
	 * @param job the job to modify
	 * @param numLines the number of lines per split
	 */
	public static void setNumLinesPerSplit(Job job, int numLines) {
		job.getConfiguration().setInt(LINES_PER_MAP, numLines);
	}

	/**
	 * Get the number of lines per split
	 * @param job the job
	 * @return the number of lines per split
	 */
	public static int getNumLinesPerSplit(JobContext job) {
		return job.getConfiguration().getInt(LINES_PER_MAP, 1);
	}
	
	/**
	 * Set the number of rows per split
	 * @param job the job to modify
	 * @param numRows the number of rows per split
	 */
	public static void setNumRowsPerSplit(Job job, int numRows) {
		job.getConfiguration().setInt(ROWS_PER_MAP, numRows);
	}

	/**
	 * Get the number of rows per split
	 * @param job the job
	 * @return the number of rows per split
	 */
	public static int getNumRowsPerSplit(JobContext job) {
		return job.getConfiguration().getInt(ROWS_PER_MAP, 1);
	}
}